package ee.maoinvest.frontend;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import ee.maoinvest.GeneralConstants;

/**
 * Servlet implementation class ProoviServletFormMassiivile
 */
@WebServlet("/ProoviServletFormMassiivile")
public class ProoviServletFormMassiivile extends HttpServlet 
{
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(ProoviServletFormMassiivile.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProoviServletFormMassiivile() 
    {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		request.setCharacterEncoding(GeneralConstants.ENCODING_UTF8);
		response.setContentType(GeneralConstants.CONTENT_TYPE_HTML_UTF8);
		
		String formSubmitted = request.getParameter("formSubmitted");
		List<Nimi> vormimassiivistSaadudNimedeMassiiv = null;
		
		if (formSubmitted != null && formSubmitted.equals("nimedeProovivorm"))
		{	
			vormimassiivistSaadudNimedeMassiiv = new ArrayList<Nimi>();
			
			String[] eesnimedeMassiiv = request.getParameterValues("nimi[][eesnimi]");
			String[] perenimedeMassiiv = request.getParameterValues("nimi[][perenimi]");
			
				for (int i = 0; i < eesnimedeMassiiv.length; i++)
				{
					Nimi nimi = new Nimi();
					nimi.setEesnimi(eesnimedeMassiiv[i]);
					nimi.setPerenimi(perenimedeMassiiv[i]);
					logger.info(eesnimedeMassiiv[i]);
					logger.info(perenimedeMassiiv[i]);
					
					vormimassiivistSaadudNimedeMassiiv.add(nimi);
				}
		}
		
		request.setAttribute("vormimassiivistSaadudNimedeMassiiv", vormimassiivistSaadudNimedeMassiiv);
		
		ServletContext servletContext = getServletConfig().getServletContext();
		servletContext.getRequestDispatcher("/menuElements/proovikasFormMassiivideSaatmine.jsp").forward(request, response);
	}

}
